/************************************************************************************/
/*  Program name CAPFNDIMP_VALUES.sql                                               */
/*  Creater:  WONG Wing Kin (PCCW - UniHub)                                         */
/*************   History   *************************                                */
/* 30 NOV, 2005   WONG Wing Kin         First created                               */
/************************************************************************************/
SET SERVEROUTPUT ON SIZE 1000000;
/*
ALTER TRIGGER FND_FLEX_VALUES_AD DISABLE;
ALTER TRIGGER FND_FLEX_VALUES_AI DISABLE;
ALTER TRIGGER FND_FLEX_VALUES_AU DISABLE;
*/
ALTER TRIGGER FND_FLEX_VALUES_CT1 DISABLE;

DECLARE
	lv_Prog_ID		VARCHAR2(100)	:=	'CAPFNDIMP_VALUES';

	lv_Scc_Count	NUMBER			:=	0;
	lv_Err_Count	NUMBER			:=	0;
	lv_Err_Msg		VARCHAR2(500)	:=	'No Error';

	CURSOR Cur_F IS
	SELECT
		*
	FROM
		CRST_FA_VALUES;

	CURSOR Cur_G IS
	SELECT
		A.*,
		B.Flex_Value_ID_N
	FROM
		CFND_FLEX_VALUES		A,
		CRST_FA_VALUES_MAP		B
	WHERE	A.Flex_Value_ID	=	B.Flex_Value_ID;

	lv_Next_Seq_ID	NUMBER;

BEGIN

	FOR C1 IN Cur_F LOOP
		BEGIN
			SELECT	FND_FLEX_VALUES_S.NextVal
			INTO	lv_Next_Seq_ID
			FROM	DUAL;
			
			UPDATE FND_FLEX_VALUES
			SET FLEX_VALUE = CAPBRIMP_MAPPING.RenameStr(Substr(FLEX_VALUE,1,4),FLEX_VALUE)
			WHERE FLEX_VALUE_ID = C1.FLEX_VALUE_ID;

			INSERT INTO CRST_FA_VALUES_MAP VALUES
			(
			C1.FLEX_VALUE_SET_ID,
			C1.FLEX_VALUE_ID,
			C1.FLEX_VALUE,
			C1.PARENT_FLEX_VALUE_LOW,
			CAPBRIMP_MAPPING.GetFNDValueSetIDMapping(C1.FLEX_VALUE_SET_ID),
			lv_Next_Seq_ID,
			C1.FLEX_VALUE,
			C1.PARENT_FLEX_VALUE_LOW
			);

			INSERT INTO FND_FLEX_VALUES
			VALUES
			(
			C1.FLEX_VALUE_SET_ID,		--FLEX_VALUE_SET_ID
			lv_Next_Seq_ID,														--FLEX_VALUE_ID,
			C1.FLEX_VALUE,
			C1.LAST_UPDATE_DATE,
			CAPBRIMP_COMMON.GetUser(C1.LAST_UPDATED_BY_NAME),					--LAST_UPDATED_BY,
			C1.CREATION_DATE,
			CAPBRIMP_COMMON.GetUser(C1.CREATED_BY_NAME),						--CREATED_BY,
			C1.LAST_UPDATE_LOGIN,
			C1.ENABLED_FLAG,
			C1.SUMMARY_FLAG,
			C1.START_DATE_ACTIVE,
			C1.END_DATE_ACTIVE,
			C1.PARENT_FLEX_VALUE_LOW,
			C1.PARENT_FLEX_VALUE_HIGH,
			C1.STRUCTURED_HIERARCHY_LEVEL,
			C1.HIERARCHY_LEVEL,
			REPLACE(C1.COMPILED_VALUE_ATTRIBUTES,'^C10^',CHR(10)),
			C1.VALUE_CATEGORY,
			C1.ATTRIBUTE1,
			C1.ATTRIBUTE2,
			C1.ATTRIBUTE3,
			C1.ATTRIBUTE4,
			C1.ATTRIBUTE5,
			C1.ATTRIBUTE6,
			C1.ATTRIBUTE7,
			C1.ATTRIBUTE8,
			C1.ATTRIBUTE9,
			C1.ATTRIBUTE10,
			C1.ATTRIBUTE11,
			C1.ATTRIBUTE12,
			C1.ATTRIBUTE13,
			C1.ATTRIBUTE14,
			C1.ATTRIBUTE15,
			C1.ATTRIBUTE16,
			C1.ATTRIBUTE17,
			C1.ATTRIBUTE18,
			C1.ATTRIBUTE19,
			C1.ATTRIBUTE20,
			C1.ATTRIBUTE21,
			C1.ATTRIBUTE22,
			C1.ATTRIBUTE23,
			C1.ATTRIBUTE24,
			C1.ATTRIBUTE25,
			C1.ATTRIBUTE26,
			C1.ATTRIBUTE27,
			C1.ATTRIBUTE28,
			C1.ATTRIBUTE29,
			C1.ATTRIBUTE30,
			C1.ATTRIBUTE31,
			C1.ATTRIBUTE32,
			C1.ATTRIBUTE33,
			C1.ATTRIBUTE34,
			C1.ATTRIBUTE35,
			C1.ATTRIBUTE36,
			C1.ATTRIBUTE37,
			C1.ATTRIBUTE38,
			C1.ATTRIBUTE39,
			C1.ATTRIBUTE40,
			C1.ATTRIBUTE41,
			C1.ATTRIBUTE42,
			C1.ATTRIBUTE43,
			C1.ATTRIBUTE44,
			C1.ATTRIBUTE45,
			C1.ATTRIBUTE46,
			C1.ATTRIBUTE47,
			C1.ATTRIBUTE48,
			C1.ATTRIBUTE49,
			C1.ATTRIBUTE50
			);
		EXCEPTION
			WHEN OTHERS THEN
				DBMS_OUTPUT.Put_Line(SQLERRM);
				lv_Err_Count	:=	lv_Err_Count + 1;
		END;
		lv_Scc_Count	:=	lv_Scc_Count	+	1;
	END LOOP;

	BEGIN
		FOR C2 IN Cur_G LOOP
			INSERT INTO CFND_FLEX_VALUES VALUES
			(
				C2.Flex_Value_ID_N,
				CAPBRIMP_MAPPING.GetOrgMapping(C2.S_ORG_ID),
				CAPBRIMP_MAPPING.GetBGMapping(C2.S_BUSINESS_GROUP_ID)
			);
		END LOOP;
	EXCEPTION
		WHEN OTHERS THEN
			DBMS_OUTPUT.Put_Line(SQLERRM);
			lv_Err_Count	:=	lv_Err_Count + 1;
	END;

	IF	lv_Err_Count	>	0	THEN
		ROLLBACK;
	END IF;

	INSERT INTO CAPCNV_LOG(
		PROG_ID,
		PROCESS_DATE,
		RECORD_PROCESSED,
		RECORD_REJECTED,
		RECORD_EXTRACTED,
		ERR_MSG
	) VALUES (
		lv_Prog_ID,
		SYSDATE,
		lv_Scc_Count,
		lv_Err_Count,
		lv_Scc_Count-lv_Err_Count,
		lv_Err_Msg
	);

	--COMMIT;
END;
/
/*
ALTER TRIGGER FND_FLEX_VALUES_AD ENABLE;
ALTER TRIGGER FND_FLEX_VALUES_AI ENABLE;
ALTER TRIGGER FND_FLEX_VALUES_AU ENABLE;
*/
ALTER TRIGGER FND_FLEX_VALUES_CT1 ENABLE;
